package com.scalar.forecast.forecastEntry.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastEntry.forms.ForecastEntrySearchForm;
import com.scalar.forecast.forecastEntry.dao.ForecastEntrySearchDAO;
import com.scalar.forecast.forecastEntry.SearchFieldListComparator;
import com.scalar.forecast.forecastSetup.ForecastTimeFrame;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.forecast.commons.Constants;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * User: Sujan Kumar
 * Date: Feb 14, 2005
 * Time: 9:45:31 AM
 */
public class ForecastEntrySearchLoadValuesCommand extends ActionCommand
{
    public  ActionForward execute ( ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response )
    {
        ForecastEntrySearchForm forecastEntrySearchForm = ( ForecastEntrySearchForm )form;
        ArrayList searchFieldList = new ArrayList();
		ArrayList forecastSetupFields = new ArrayList();
		boolean isOrderBySet = false;
		Calendar  now = Calendar.getInstance();
        try
        {
            forecastSetupFields = ForecastSetupDAO.getForecastSetupFields(null);
            request.setAttribute ( "forecastSetupFields", forecastSetupFields );
			String orderBy = forecastEntrySearchForm.getOrderBy();
			if ( orderBy == null || orderBy.trim().length() == 0 )
			{
				isOrderBySet = true;
				forecastEntrySearchForm.setOrderBy( ForecastEntrySearchNewCommand.getDefaultOrderBy ( forecastSetupFields ) );
			}
            searchFieldList = ForecastEntrySearchDAO.getForecastSetupSearchFields(null);
            ForecastEntrySearchNewCommand.loadSearchFieldListPropertyValues(forecastEntrySearchForm,searchFieldList);
        }
        catch ( Exception e )
        {
            System.out.println ( "----------exception in search new Command:" );
            e.printStackTrace();
        }
        ForecastEntrySearchNewCommand.setSortOrder ( forecastEntrySearchForm.getOrderBy(), searchFieldList );
        Collections.sort( searchFieldList, new SearchFieldListComparator() );
        forecastEntrySearchForm.setSearchFieldList( searchFieldList );

        try
        {
            ForecastTimeFrame timeframe = ForecastSetupDAO.getForecastTimeFrame(null);
            //this request field is used to display the label after the showNext textfield.
            if ( "Month".equalsIgnoreCase( timeframe.getTfName() ) )
            {
				if ( isOrderBySet )
				{
					forecastEntrySearchForm.setStartMonth_Quarter_Year( now.get( Calendar.MONTH ) );
                	forecastEntrySearchForm.setShowNext(6);
				}
                request.setAttribute ( "showNext", "Months" );
            }
            else if ( "Quarter".equalsIgnoreCase( timeframe.getTfName() ) )
            {
				if ( isOrderBySet )
				{
					int currentQuarter = -1;
					try
					{
						currentQuarter = ForecastEntrySearchDAO.getCurrentQuarter(null);
					}
					catch ( Exception e)
					{
						System.out.println ( "--Exception in getting the current Quarter---" );
						e.printStackTrace();
					}
					forecastEntrySearchForm.setStartMonth_Quarter_Year( currentQuarter );
					forecastEntrySearchForm.setShowNext(4);
				}
                request.setAttribute ( "showNext", "Quarters" );
            }
            else if ( "Year".equalsIgnoreCase( timeframe.getTfName() ) )
            {
				if ( isOrderBySet )
				{
					forecastEntrySearchForm.setStartMonth_Quarter_Year( now.get( Calendar.YEAR ) );
					forecastEntrySearchForm.setShowNext(3);
				}
                request.setAttribute ( "showNext", "Years" );
            }
        }
        catch ( Exception e )
        {
            System.out.println ( "----------exception while getting the timeframe-----");
            e.printStackTrace();
        }
        request.setAttribute ( Constants.FORECAST_IS_BUILD, request.getParameter( Constants.FORECAST_IS_BUILD ) );
        ActionForward actionForward = mapping.findForward ( "searchPage" );
        return actionForward;
    }

}
